/**
 * 
 */
package any.tag.server.resource;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.util.Collection;

import javax.activation.FileTypeMap;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.Variant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import any.restlet.helper.RepresentationHelper;
import any.tag.TagItem;
import any.tag.server.TagService;

/**
 * TODO: Doc me
 * 
 * @author keke <keke@codehaus.org>
 * @version
 * @revision $Revision:$
 */
public class TagAboutResource extends Resource {
    private static final Logger  LOG = LoggerFactory
                                             .getLogger(TagAboutResource.class);
    private URI                  about;
    private RepresentationHelper repHelper;
    private Collection<TagItem> tagItems;
    private TagService           tagService;
    private String               type;

    @Override
    public void init(final Context context, final Request request,
            final Response response) {
        super.init(context, request, response);
        final String aboutStr = (String) request.getAttributes().get("about");
        try {
            Validate.isTrue(!StringUtils.isEmpty(aboutStr));            
            about = new URI(URLDecoder.decode(aboutStr, "UTF-8"));
            type = (String) getRequest().getAttributes().get("type");
            tagItems = tagService.getTagItemsByAbout(about);
            if (StringUtils.isEmpty(type)) {
                type = "html";
            }
            getVariants().add(
                    new Variant(MediaType.valueOf(FileTypeMap
                            .getDefaultFileTypeMap().getContentType(
                                    "this." + type))));
        } catch (final URISyntaxException e) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Bad URI: {}", aboutStr);
            }
            response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        } catch (final UnsupportedEncodingException e) {
            if (LOG.isWarnEnabled()) {
                LOG.warn("Bad URI: {}", aboutStr);
            }
            response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        }
    }

    @Override
    public Representation represent(final Variant variant)
            throws ResourceException {
        final Representation rep = repHelper.toRepresentation("tag", tagItems,
                variant);
        if (rep == null) {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE,
                    "Type " + variant.getMediaType());
        }
        return rep;
    }

    public void setRepHelper(final RepresentationHelper repHelper) {
        this.repHelper = repHelper;
    }

    public void setTagService(final TagService tagService) {
        this.tagService = tagService;
    }
}
